﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebServiceClient
{
    public partial class Form1 : Form
    {
        public static List<Algorithm> algorithms = new List<Algorithm>();
        private static bool auth = false;

        private static string login;
        private static string password;
        private static string name;

        public static string Login
        { get { return login; } }

        public static string Password
        { get { return password; } }

        public static string Name
        { get { return name; } }

        private static int currentId;
        private static Helpers help = new Helpers();
        public UploadForm uploadF;
        public RunForm runF;

        public Form1()
        {
            InitializeComponent();
            uploadF = new UploadForm();
            runF = new RunForm();
        }

        private void SignInButton_Click(object sender, EventArgs e)
        {
            if (help.Authorize(LoginBox.Text, PasswordBox.Text))
            {
                SignInButton.Enabled = false; //disable
                SignInMessage.ForeColor = Color.Green;
                SignInMessage.Text = "You have successfully signed in";
                auth = true;
                var alg = help.GetAlgorithms(LoginBox.Text, PasswordBox.Text);
                for (int i = 0; i < alg.Count; i++)
                {
                    AlgorithmsTree.Nodes.Add(alg[i].Name);
                }
                UploadButton.Enabled = true;
                RefreshButton.Enabled = true;
                login = LoginBox.Text;
                password = PasswordBox.Text;
                SignOutButton.Enabled = true;
                LoginBox.Enabled = false;
                PasswordBox.Enabled = false;
            }
            else
            {
                SignInMessage.ForeColor = Color.Red;
                SignInMessage.Text = "You entered an incorrect login or password";
                login = string.Empty;
                password = string.Empty;

            }
        }

        private void AlgorithmsTree_DoubleClick(object sender, EventArgs e)
        {
            var index = AlgorithmsTree.SelectedNode.Index;
            currentId = index;
            nameBox.Text = AlgorithmsTree.Nodes[index].Text;

            if (algorithms[index].CanEdit)
            {
                descriptionBox.Text =
                    help.GetAlgorithmDescriptionByName(LoginBox.Text, PasswordBox.Text, AlgorithmsTree.Nodes[index].Text);
                editBox.Text
                    = help.GetAlgorithmByName(LoginBox.Text, PasswordBox.Text, AlgorithmsTree.Nodes[index].Text);

                SaveButton.Enabled = true;
                //RemoveButton.Enabled = true;
            }
            else
            {
                descriptionBox.Text = string.Empty;
                editBox.Text = string.Empty;
                SaveButton.Enabled = false;
            }
            RunButton.Enabled = true;
            LogMessage.Text = "The algorithm has been successfully loaded";
            name = AlgorithmsTree.Nodes[index].Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            uploadF.Show();
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            help.SaveAlgorithm(LoginBox.Text, PasswordBox.Text, AlgorithmsTree.Nodes[currentId].Text, nameBox.Text, descriptionBox.Text, editBox.Text);
            AlgorithmsTree.Nodes[currentId].Text = nameBox.Text;           
            algorithms[currentId].Name = AlgorithmsTree.Nodes[currentId].Text;
            LogMessage.Text = "The algorithm has been successfully saved";
        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            help.RemoveAlgorithm(LoginBox.Text, PasswordBox.Text, AlgorithmsTree.Nodes[currentId].Text);
        }

        private void RunButton_Click(object sender, EventArgs e)
        {
            runF.Show();
        }

        private void RefreshNutton_Click(object sender, EventArgs e)
        {
            if (auth)
            {
                AlgorithmsTree.Nodes.Clear();
                var alg = help.GetAlgorithms(LoginBox.Text, PasswordBox.Text);
                for (int i = 0; i < alg.Count; i++)
                {
                    AlgorithmsTree.Nodes.Add(alg[i].Name);
                }
            }
        }

        private void SignOutButton_Click(object sender, EventArgs e)
        {
            auth = false;
            SignInMessage.ForeColor = Color.Blue;
            SignInMessage.Text = "To enter your login and password to sign in";
            UploadButton.Enabled = false;
            RefreshButton.Enabled = false;
            SignInButton.Enabled = true;
            LoginBox.Enabled = true;
            PasswordBox.Enabled = true;
            AlgorithmsTree.Nodes.Clear();
            SignOutButton.Enabled = false;
            nameBox.Text = string.Empty;
            descriptionBox.Text = string.Empty;
            editBox.Text = string.Empty;
            SaveButton.Enabled = false;
            RemoveButton.Enabled = false;
            UploadButton.Enabled = false;
            RunButton.Enabled = false;
        }
    }
}
